package org.netty.demo.lihh.notice.strategy.impl;

import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;
import org.netty.demo.lihh.notice.domain.ClientMsgProtocol;
import org.netty.demo.lihh.notice.strategy.StrategyHandle;
import org.netty.demo.lihh.notice.util.ChannelHandler;
import org.netty.demo.lihh.notice.util.MsgUtil;

import java.util.ArrayList;
import java.util.List;

public class LogoutStrategyImpl implements StrategyHandle {
  
  @Override
  public void accept(ClientMsgProtocol dto) {
    String roleId = dto.getRoleId(), channelId = dto.getChannelId(), tk = dto.getTk();
    
    // chanel 记录
    List<Channel> channelRecords = ChannelHandler.getChannelExcludeChannelId(roleId, new ArrayList<>());
    // 持久化 roleId、tk，channelId，channel 之间的关系
    ChannelHandler.addChannel(roleId, tk, channelId);
    
    channelRecords.forEach(channel -> {
      SocketChannel socketChannel = (SocketChannel) channel;
      socketChannel.writeAndFlush(MsgUtil.buildLogoutResponseMsg(tk, channelId));
    });
  }
}
